package com.ruoyi.sales.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.ZcStock;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sales.domain.ZcStoreInfo;
import com.ruoyi.sales.service.IZcStockService;
import com.ruoyi.sales.service.IZcStoreInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sales.domain.ZcStoreMaterial;
import com.ruoyi.sales.service.IZcStoreMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 原料仓库Controller
 * 
 * @author woo
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/sales/material")
public class ZcStoreMaterialController extends BaseController
{
    @Autowired
    private IZcStoreMaterialService zcStoreMaterialService;

    @Autowired
    private IZcStoreInfoService storeInfoService;

    @Autowired
    private IZcStockService stockService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询原料仓库列表
     */
    @PreAuthorize("@ss.hasPermi('sales:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZcStoreMaterial zcStoreMaterial)
    {
        startPage();
        List<ZcStoreMaterial> list = zcStoreMaterialService.selectZcStoreMaterialList(zcStoreMaterial);
        for (ZcStoreMaterial storeMaterial : list) {
            //仓库
            ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(storeMaterial.getStoreId());
            storeMaterial.setStoreName(zcStoreInfo.getStoreName());
            //原料
            ZcStock zcStock = stockService.selectStockById(storeMaterial.getStockId());
            storeMaterial.setStockName(zcStock.getStockName());
            //采购人
            SysUser sysUser = userService.selectUserById(storeMaterial.getStaffId());
            storeMaterial.setStaffName(sysUser.getUserName());
            //仓储容量
//            String produceCount = zcStoreInfo.getProduceCount();
//            int storeCount = zcStoreMaterialService.selectCountByStore(zcStoreMaterial.getStoreId());
//            int materialCount = Integer.valueOf(zcStoreMaterial.getMaterialCount());
//            zcStoreMaterial.setLastCount();
        }
        return getDataTable(list);
    }

    /**
     * 导出原料仓库列表
     */
    @PreAuthorize("@ss.hasPermi('sales:material:export')")
    @Log(title = "原料仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcStoreMaterial zcStoreMaterial)
    {
        List<ZcStoreMaterial> list = zcStoreMaterialService.selectZcStoreMaterialList(zcStoreMaterial);
        ExcelUtil<ZcStoreMaterial> util = new ExcelUtil<ZcStoreMaterial>(ZcStoreMaterial.class);
        util.exportExcel(response, list, "原料仓库数据");
    }

    /**
     * 获取原料仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return success(zcStoreMaterialService.selectZcStoreMaterialByMaterialId(materialId));
    }

    /**
     * 新增原料仓库
     */
    @Transactional
    @PreAuthorize("@ss.hasPermi('sales:material:add')")
    @Log(title = "原料仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcStoreMaterial zcStoreMaterial)
    {

        ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(zcStoreMaterial.getStoreId());
        long storeCount = zcStoreInfo.getTrueCount() + zcStoreInfo.getUntrueCount();
        long count = zcStoreInfo.getLine()*zcStoreInfo.getWide() - storeCount - Long.valueOf(zcStoreMaterial.getMaterialCount());
        long produceCount = zcStoreInfo.getLine()*zcStoreInfo.getWide() - storeCount;

        if (count<0){
            return AjaxResult.error("当前仓储容量为"+produceCount+",仓储不足,请重新选择合适仓库");
        }
        if (count==0){
            zcStoreInfo.setStatus("1");
        }

        zcStoreInfo.setTrueCount(zcStoreInfo.getTrueCount()+Long.valueOf(zcStoreMaterial.getMaterialCount()));
        storeInfoService.updateZcStoreInfo(zcStoreInfo);
        return toAjax(zcStoreMaterialService.insertZcStoreMaterial(zcStoreMaterial));

//        int storeCount = zcStoreMaterialService.selectCountByStore(zcStoreMaterial.getStoreId());
//        int materialCount = Integer.valueOf(zcStoreMaterial.getMaterialCount());
//        ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(zcStoreMaterial.getStoreId());
//        int produceCount = (int)(zcStoreInfo.getLine()*zcStoreInfo.getWide());
//        int lastCount = produceCount - materialCount - storeCount;
//        int count = produceCount - materialCount;
//        if (lastCount<0){
//            return AjaxResult.error("当前仓储容量为"+count+",仓储不足");
//        }
//        zcStoreMaterial.setLastCount(String.valueOf(lastCount));
//        return toAjax(zcStoreMaterialService.insertZcStoreMaterial(zcStoreMaterial));
    }

    /**
     * 修改原料仓库
     */
    @PreAuthorize("@ss.hasPermi('sales:material:edit')")
    @Log(title = "原料仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcStoreMaterial zcStoreMaterial)
    {
        return toAjax(zcStoreMaterialService.updateZcStoreMaterial(zcStoreMaterial));
    }

    /**
     * 删除原料仓库
     */
    @PreAuthorize("@ss.hasPermi('sales:material:remove')")
    @Log(title = "原料仓库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(zcStoreMaterialService.deleteZcStoreMaterialByMaterialIds(materialIds));
    }

    /**
     * 获取仓库列表
     */
    @PreAuthorize("@ss.hasPermi('sales:material:list')")
    @GetMapping("/storeList")
    public AjaxResult storeList(){
        List<ZcStoreInfo> stockStore = storeInfoService.selectAllStockStore();
        return success(stockStore);
    }


}
